﻿namespace OxWebCATI.Monitoring
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Runtime.InteropServices;

    [ComVisible(true), Guid("6BBAB81B-73EA-4402-955D-8190284DC140")]
    public class DataContainer
    {
        private static SortedList<string, UserMonitorData> _Cache = new SortedList<string, UserMonitorData>();
        private static List<string> _MonitoredUsers = new List<string>();

        [ComVisible(true)]
        public UserMonitorData Get(string user)
        {
            return this[user];
        }

        [ComVisible(true)]
        public bool IsBeingMonitored(string user)
        {
            if (string.IsNullOrEmpty(user))
            {
                return false;
            }
            return _MonitoredUsers.Contains(user);
        }

        [ComVisible(true)]
        public void StartMonitoring(string user)
        {
            if (!string.IsNullOrEmpty(user) && !_MonitoredUsers.Contains(user))
            {
                _MonitoredUsers.Add(user);
            }
        }

        [ComVisible(true)]
        public void StopMonitoring(string user)
        {
            if (!string.IsNullOrEmpty(user) && _MonitoredUsers.Contains(user))
            {
                _Cache.Remove(user);
                _MonitoredUsers.Remove(user);
            }
        }

        [ComVisible(true)]
        public UserMonitorData this[string user]
        {
            get
            {
                if (!(!string.IsNullOrEmpty(user) && this.IsBeingMonitored(user)))
                {
                    return null;
                }
                if (!_Cache.ContainsKey(user))
                {
                    _Cache.Add(user, new UserMonitorData());
                }
                return _Cache[user];
            }
        }
    }
}

